"""
@author: 江同学呀
@file: metadata_fields.py
@date: 2024/7/29 21:58
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/mapping-fields.html
"""
import enum
from typing import Optional


class MetadataType(enum.Enum):
    """
    元数据类型
    """
    index = "index"  # 索引
    result = "result"  # 结果


class BaseMetadataField(object):
    """
    元数据字段基类
    """
    def __init__(self, *args, **kwargs):
        for k, v in kwargs.items():
            if hasattr(self, f"_{k}"):
                setattr(self, f"_{k}", v)
        return


class IdentityMetadataField(BaseMetadataField):
    """
    标识元数据字段
    """
    _index: str = "_index"
    """
    The index to which the document belongs.
    文档所属的索引。
    """
    _type: str = "_type"
    """
    The document’s mapping type.
    文档的映射类型。
    """
    _id: str = "_id"
    """
    The document’s ID.  
    文档的 ID。
    """

    @property
    def index(self):
        return self._index

    @property
    def type(self):
        return self._type

    @property
    def id(self):
        return self._id


class DocumentSourceMetadataField(BaseMetadataField):
    """
    文档源元数据字段
    """
    _source: str = "_source"
    """
    The original JSON representing the body of the document.
    表示文档正文的原始 JSON。
    """
    _size: str = "_size"
    """
    The size of the _source field in bytes, provided by the mapper-size plugin.
    _source字段的大小（以字节为单位），由 mapper-size 插件提供。
    """

    @property
    def source(self):
        return self._source

    @property
    def size(self):
        return self._size


class DocCountMetadataField(BaseMetadataField):
    """
    文档计数元数据字段
    """
    _doc_count: str = "_doc_count"
    """
    A custom field used for storing doc counts when a document represents pre-aggregated data.
    当文档表示预先聚合的数据时，用于存储文档计数的自定义字段。
    """

    @property
    def doc_count(self):
        return self._doc_count


class IndexingMetadataField(BaseMetadataField):
    """
    索引元数据字段
    """
    _field_names: str = "_field_names"
    """
    All fields in the document which contain non-null values.
    文档中包含非 null 值的所有字段。
    """
    _ignored: str = "_ignored"
    """
    All fields in the document that have been ignored at index time because of ignore_malformed.
    文档中由于ignore_malformed而在索引时被忽略的所有字段。
    """

    @property
    def field_names(self):
        return self._field_names

    @property
    def ignored(self):
        return self._ignored


class RoutingMetadataField(BaseMetadataField):
    """
    路由元数据字段
    """
    _routing: str = "_routing"
    """
    A custom routing value which routes a document to a particular shard.
    一个自定义路由值，用于将文档路由到特定分片。
    """

    @property
    def routing(self):
        return self._routing


class OtherMetadataField(BaseMetadataField):
    """
    其他元数据字段
    """
    _meta: str = "_meta"
    """
    Application specific metadata.
    特定于应用程序的元数据。
    """
    _tier: str = "_tier"
    """
    The current data tier preference of the index to which the document belongs.
    文档所属索引的当前数据层首选项。
    """

    @property
    def meta(self):
        return self._meta

    @property
    def tier(self):
        return self._tier


class MetadataField(
    IdentityMetadataField, DocumentSourceMetadataField, DocCountMetadataField, IndexingMetadataField,
    RoutingMetadataField, OtherMetadataField
):
    """
    元数据字段
    """
    def __init__(self, metadata_type: Optional[MetadataType] = None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._metadata_type: Optional[MetadataType] = metadata_type
        return

    @property
    def metadata_type(self) -> Optional[MetadataType]:
        return self._metadata_type




